package com.sduept.nwld.dataserver.controller.fault;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.extend.LineSegment;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.cim.model.wires.DcLinesegment;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.Date;
import java.util.List;

@Named
@ViewScoped
public class FaultCenterController extends AbstractController {
	private static final long serialVersionUID = -7252438046347439930L;
	@Autowired
	private CimManager cm;
	@Autowired
	private FaultRecordQueryManager frqm;
	private String faultTime = null;
	private String deviceId = null;
	private String deciceName = null;
	private FaultRecord record;
	private String url = null;
	private String param = "";
	private String showBackButton = null;

	/**
	 * 用于故障报告页面中转，根据故障类型跳转到不同页面。
	 * 
	 * 将FaultRecord查询由使用SuperDAO改为使用 FaultRecordQueryManager查询 （20190109 zhangqi）
	 */
	@PostConstruct
	private void init() {
		deviceId = getParameter("lineId");
		showBackButton = getParameter("showBackButton");
		if (StringUtils.isEmpty(deviceId)) {
			return;
		}
		deciceName = getParameter("lineName");
		faultTime = getParameter("faultTime");
		Date dateTime = DateUtil.getDateByString(faultTime, DateUtil.pattern_ymdhms);
		List<FaultRecord> findByCondition = frqm.findEqFaultTimeFaultByTimeDeviceId(dateTime, deviceId);
		
		if (findByCondition.size() > 0) {
			record = findByCondition.get(0);
		}
		if ("线路".equals(record.getEtype())) {
			LineSegment line=cm.getLineSegmentById(deviceId);
			if(line==null)
				url = "";
			else if(line instanceof ACLineSegment)
				url = "/ui/fault/lineFaultDetails.xhtml";
			else if(line instanceof DcLinesegment)
				url = "/ui/fault/dcLineFaultDetails.xhtml";
		} else if ("变压器".equals(record.getEtype())) {
			url = "/ui/fault/transformerFaultDetails.xhtml";
		} else if ("母线".equals(record.getEtype())) {
			url = "/ui/fault/busFaultDetails.xhtml";
		} else {
			url = "";
			return;
		}
		param = "lineId=" + deviceId + "&faultTime=" + faultTime + "&lineName=" + deciceName
				+ "&showBackButton=" + showBackButton;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getShowBackButton() {
		return showBackButton;
	}

	public void setShowBackButton(String showBackButton) {
		this.showBackButton = showBackButton;
	}

}
